package leetcode.f1t100;

/**
 * 两数相除
 * https://leetcode.com/problems/divide-two-integers/
 *
 * @Author lichongxiao
 * @Date 2022/10/8 14:26
 */
public class Q29_DivideTwoIntegers {

    public int divide(int dividend, int divisor) {
        if(dividend == Integer.MIN_VALUE && divisor == -1){
            return Integer.MAX_VALUE;
        }
        int a = Math.abs(dividend), b = Math.abs(divisor), result = 0, i=0;
        while (a -b >= 0) {
            for (i = 0; a - (b << i << 1) > 0; i++);
            result += 1 << i;
            a -= b << i;
        }
        return (dividend > 0) == (divisor > 0) ? result : -result;
    }

    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE);
        System.out.println(Integer.MIN_VALUE);
        System.out.println(-Integer.MIN_VALUE);
        System.out.println(Math.abs(Integer.MIN_VALUE));
        Q29_DivideTwoIntegers bean = new Q29_DivideTwoIntegers();
        System.out.println(bean.divide(2147483647,1));
        System.out.println(bean.divide(16,2));
    }
}
